package com.lslk.othello.models;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.lslk.othello.ActivityOthello;
import com.lslk.othello.R;

public class Position extends ImageView implements OnClickListener{
	public static final String TAG="Position";
	
	public interface Occupation{
		public final static int EMPTY=0;
		public final static int WHITE=1;
		public final static int BLACK=2;
	}
	public interface Piece{
		public final static int EMPTY=R.drawable.empty;
		public final static int WHITE=R.drawable.white;
		public final static int BLACK=R.drawable.black;
		public final static int LEGAL=R.drawable.legal;
	}
	public int occupation;
	public int id;
	public boolean legal;
	public Position(ActivityOthello c,int k){
		super(c);
		occupation=Occupation.EMPTY;
		id=k;
		legal=true;
		setClickable(true);
		setImageResource(Piece.EMPTY);
		setOnClickListener(this);
	}
	public void legalize(){
		legal=true; 
		this.setImageResource(Piece.LEGAL);
	}
	public boolean makeEmpty(){
		//Log.i(TAG," Made empty");
		occupation=Occupation.EMPTY;
		setImageResource(Piece.EMPTY);
		return true;
	}
	public boolean makeWhite(){
		if(occupation==Occupation.WHITE)
			return false;
		else{
			//Log.i(TAG," Made white");
			occupation=Occupation.WHITE;
			setImageResource(Piece.WHITE);
			return true;
		}
	}
	public void makeOpposite(){
		//Log.i(TAG,"Opposite: "+this.id);
		if(occupation==Occupation.WHITE)
			this.makeBlack();
		else
			this.makeWhite();
	}
	public boolean makeBlack(){
		if(occupation==Occupation.BLACK)
			return false;
		else{
			//Log.i(TAG," Made black");
			occupation=Occupation.BLACK;
			setImageResource(Piece.BLACK);
			invalidate();
			return true;
		}
	}
	public void onClick(View arg0) {
			Position v=(Position) arg0;
			if(!v.legal)
				return;
			//Log.i(TAG,"Clicked at: "+v.occupation+" - "+v.key);
			ActivityOthello c=(ActivityOthello) this.getContext();
			if(!c.game.isStarted){
				if(v.occupation==Occupation.EMPTY){
					v.makeWhite();
					return;
				}
				if(v.occupation==Occupation.WHITE){
					v.makeBlack();
					return;
				}
				if(v.occupation==Occupation.BLACK){
					v.makeEmpty();
					return;
				}
			}else{
				if(c.game.block_human==true)
					return;
				if(c.game.blackTurn){
					v.makeBlack();
					c.game.makeMove(v.id);
				} else{
					v.makeWhite();
					c.game.makeMove(v.id);
				}
			}
	}
}
